package java0409;

class ListNode {
    public int val;
    public ListNode next;

    public ListNode(int val) {
        this.val = val;
    }
}


public class Solution {

    // 环形链表
    // 给定一个链表，判断链表中是否有环。
    public boolean hasCycle(ListNode head) {
        if (head == null) {
            return false;
        }
        ListNode slow = head;
        ListNode fast = head;
        while (true) {
            if (fast.next != null && fast.next.next != null) {
                fast = fast.next.next;
            }else {
                return false;
            }
            slow = slow.next;
            if (slow == fast) {
                return true;
            }
        }
    }
}
